package com.android.baseconfig.common.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author lei
 * @description FileMD5
 * @date 2018/5/7
 */
public final class FileMD5 {
    private FileMD5() {
    }

    private static final String TAG = FileMD5.class.getName();

    /**
     * 获取压缩文件MD5值 David 2012-10-12
     */
    private final static char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f'};

    private static MessageDigest messagedigest = null;

    static {
        try {
            messagedigest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            Logc.e(TAG, "", e);
        }
    }

    public static String getFileMD5String(String filePath) throws IOException {

        return getFileMD5String(new File(filePath));

    }

    synchronized public static String getFileMD5String(File file) throws IOException {
        FileInputStream fis = null;
        BufferedInputStream bis = null;

        try {
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);

            byte[] buf = new byte[1024 * 1024];

            int len = -1;

            while ((len = bis.read(buf)) != -1) {
                messagedigest.update(buf, 0, len);

            }
            bis.close();
            fis.close();
            return bufferToHex(messagedigest.digest());
        } catch (FileNotFoundException e) {
            Logc.e(TAG, "", e);
            throw e;
        } catch (IOException e) {
            Logc.e(TAG, "", e);
            throw e;
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
            } catch (IOException e) {
                Logc.e(TAG, "", e);
            }

            try {
                if (bis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                Logc.e(TAG, "", e);
            }
        }

    }

    public static String getMD5String(String s) {
        return getMD5String(s.getBytes(Charset.forName(SecurityUtils.GBK_ENCODING)));
    }

    public static String getMD5String(byte[] bytes) {
        messagedigest.update(bytes);
        return bufferToHex(messagedigest.digest());
    }

    private static String bufferToHex(byte[] bytes) {
        return bufferToHex(bytes, 0, bytes.length);
    }

    private static String bufferToHex(byte[] bytes, int m, int n) {
        StringBuffer stringbuffer = new StringBuffer(2 * n);

        for (int l = 0; l < n; l++) {
            appendHexPair(bytes[l + m], stringbuffer);
        }

        return stringbuffer.toString();
    }

    private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
        char c0 = HEX_DIGITS[(bt & 0xf0) >> 4];
        char c1 = HEX_DIGITS[bt & 0xf];
        stringbuffer.append(c0);
        stringbuffer.append(c1);
    }

    public static boolean checkPassword(String password, String md5PwdStr) {
        String s = getMD5String(password);
        return s.equals(md5PwdStr);
    }

}
